package com.meiyou.controller;

import com.meiyou.dto.UpdateUserDto;
import com.meiyou.dto.UserLoginDto;
import com.meiyou.dto.UserRegisterDto;
import com.meiyou.entity.User;
import com.meiyou.result.Result;
import com.meiyou.service.UserService;
import com.meiyou.vo.UserInfoVo;
import com.meiyou.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 注册
     *
     * @param userRegisterDto
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterDto userRegisterDto) {

        userService.register(userRegisterDto);

        return Result.success("注册成功！");
    }

    /**
     * 发送验证码
     *
     * @return
     */
    @PostMapping("/code")
    public Result<String> sendCode(@RequestBody User user) {

        userService.sendCode(user.getEmail());

        return Result.success("邮件发送成功！");
    }

    /**
     * 登录
     *
     * @param userLoginDto
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVo> login(@RequestBody UserLoginDto userLoginDto) {

        UserLoginVo userLoginVo = userService.login(userLoginDto);

        return Result.success(userLoginVo);
    }

    /**
     * 忘记密码，修改密码
     *
     * @param id
     * @param updateUserDto
     * @return
     */
    @PutMapping("/forget/{id}")
    public Result forgetPassword(@PathVariable Long id, @RequestBody UpdateUserDto updateUserDto) {

        userService.forgetPassword(id, updateUserDto.getPassword());

        return Result.success("修改密码成功！");
    }

    /**
     * 验证验证码是否正确、邮箱是否已注册
     *
     * @param map
     * @return
     */
    @PostMapping("/verifyCode")
    public Result<Long> verifyCode(@RequestBody Map<String, String> map) {

        Long id = userService.verifyCode(map);

        return Result.success(id);
    }

    /**
     * 修改用户信息
     *
     * @param updateUserDto
     * @return
     */
    @PutMapping
    public Result updateUserInfo(@RequestBody UpdateUserDto updateUserDto) {

        userService.updateUserInfo(updateUserDto);

        return Result.success("修改成功！");
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping
    public Result<UserInfoVo> getUserInfo() {

        UserInfoVo userInfoVo = userService.getUserInfo();

        return Result.success(userInfoVo);
    }


}

